package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.school.campus.domain.Goods;
import com.school.campus.service.impl.GoodsServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.VendingMachine;
import com.school.campus.service.IVendingMachineService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 自动售货机Controller
 *
 * @author wwj
 * @date 2025-11-04
 */
@RestController
@RequestMapping("/campus/machine")
public class VendingMachineController extends BaseController {
    @Autowired
    private IVendingMachineService vendingMachineService;
    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 查询自动售货机列表
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:list')")
    @GetMapping("/list")
    public TableDataInfo list(VendingMachine vendingMachine) {
        startPage();
        List<VendingMachine> list = vendingMachineService.selectVendingMachineList(vendingMachine);
        return getDataTable(list);
    }

    /**
     * 导出自动售货机列表
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:export')")
    @Log(title = "自动售货机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VendingMachine vendingMachine) {
        List<VendingMachine> list = vendingMachineService.selectVendingMachineList(vendingMachine);
        ExcelUtil<VendingMachine> util = new ExcelUtil<VendingMachine>(VendingMachine.class);
        util.exportExcel(response, list, "自动售货机数据");
    }

    /**
     * 获取自动售货机详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(vendingMachineService.selectVendingMachineById(id));
    }

    /**
     * 新增自动售货机
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:add')")
    @Log(title = "自动售货机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VendingMachine vendingMachine) {
        return toAjax(vendingMachineService.insertVendingMachine(vendingMachine));
    }

    /**
     * 修改自动售货机
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:edit')")
    @Log(title = "自动售货机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VendingMachine vendingMachine) {
        return toAjax(vendingMachineService.updateVendingMachine(vendingMachine));
    }

    /**
     * 删除自动售货机
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:remove')")
    @Log(title = "自动售货机", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vendingMachineService.deleteVendingMachineByIds(ids));
    }

    /**
     * 查看售货机货物
     */
    @PreAuthorize("@ss.hasPermi('campus:machine:query')")
    @GetMapping( "/{id}/goods")
    public AjaxResult getGoodsByMachineId(@PathVariable("id") Long id) {

        Goods goods = new Goods();
        goods.setVendingMachineId(id);
        return success(goodsService.selectGoodsList(goods));
    }
}
